package com.alliky.core.binding.viewadapter.smartrefresh;

import androidx.annotation.NonNull;
import androidx.databinding.BindingAdapter;

import com.alliky.core.binding.command.BindingCommand;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

public class ViewAdapter {

    //下拉刷新命令
    @BindingAdapter({"onRefreshCommand"})
    public static void onRefreshCommand(SmartRefreshLayout smartRefreshLayout, final BindingCommand onRefreshCommand) {
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                if (onRefreshCommand != null) {
                    onRefreshCommand.execute();
                }
            }
        });
    }

    //上拉加载
    @BindingAdapter({"onLoadMoreCommand"})
    public static void onLoadMoreCommand(SmartRefreshLayout smartRefreshLayout, final BindingCommand onLoadMoreCommand) {
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (onLoadMoreCommand != null) {
                    onLoadMoreCommand.execute();
                }
            }
        });
    }



    @BindingAdapter({"onRefreshLoadMoreCommand"})
    public static void onRefreshLoadMoreCommand(SmartRefreshLayout smartRefreshLayout, final BindingCommand onRefreshLoadMoreCommand) {
        smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (onRefreshLoadMoreCommand != null) {
                    onRefreshLoadMoreCommand.execute();
                }
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                if (onRefreshLoadMoreCommand != null) {
                    onRefreshLoadMoreCommand.execute();
                }
            }
        });
    }

    @BindingAdapter({"onEnableRefreshCommand"})
    public static void onEnableRefreshCommand(SmartRefreshLayout smartRefreshLayout, final boolean onEnableRefreshCommand) {
        smartRefreshLayout.setEnableRefresh(onEnableRefreshCommand);
    }

    @BindingAdapter({"onEnableLoadMoreCommand"})
    public static void onEnableLoadMoreCommand(SmartRefreshLayout smartRefreshLayout, final boolean onEnableLoadMoreCommand) {
        smartRefreshLayout.setEnableLoadMore(onEnableLoadMoreCommand);
    }

}
